package com.topscomm.mpm.script;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectMilepostEntity;
import com.topscomm.mpm.pojo.MpmProjectTaskCloseEntity;
import com.topscomm.mpm.pojo.MpmProjectTaskEntity;
import com.topscomm.mpm.service.impl.MpmProjectMilepostService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.mpm.service.impl.MpmProjectTaskCloseService;
import com.topscomm.mpm.service.impl.MpmProjectTaskService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

public class MpmProjectTaskCloseScript extends MpmScript {

	/**
	 * 任务反馈结束后更新对应任务相关字段，对应里程碑及任务字段，对应项目字段
	 *
	 * @param service
	 * @param entity
	 * @throws Exception
	 */
	@Transactional
	public void projectTaskUpdate(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		MpmProjectTaskCloseService mpmProjectTaskCloseService = (MpmProjectTaskCloseService) SpringUtil.getBean("mpmProjectTaskCloseService");
		MpmProjectTaskService mpmProjectTaskService = (MpmProjectTaskService) SpringUtil.getBean("mpmProjectTaskService");
		MpmProjectMilepostService mpmProjectMilepostService = (MpmProjectMilepostService) SpringUtil.getBean("mpmProjectMilepostService");
		MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
		// 查询项目任务关闭所属任务
		MpmProjectTaskCloseEntity mpmProjectTaskCloseEntity = mpmProjectTaskCloseService.queryById(sourceid);
		String taskid = ConvertUtil.convertToString(mpmProjectTaskCloseEntity.getTaskid());
		MpmProjectTaskEntity mpmProjectTaskEntity = mpmProjectTaskService.queryById(taskid);
		// 更新项目任务字段
		String taskstate = String.valueOf(mpmProjectTaskCloseEntity.getTaskstate());
		mpmProjectTaskEntity.setTaskstate(taskstate);
		mpmProjectTaskService.updateEntity(mpmProjectTaskEntity);
		// 若任务关闭则判断以下事情：是否需要更新项目状态；是否需要更新里程碑及任务状态
		String milepostCode = mpmProjectTaskEntity.getMilepostcode();
		StringBufferProxy whereSql = new StringBufferProxy();
		String projectCode = mpmProjectTaskEntity.getProjectcode();
		// 任务已完成需要执行实时更新项目状态方法（方法会判断是否需要更新）
		mpmProjectService.updateAbnormalState(projectCode);
		// 查询该任务所属里程碑及任务下是否还有非已完成/已关闭的任务
		whereSql.appendLineSingle(" {0} = '{1}' and {2} in ('{3}','{4}') ", MpmProjectTaskEntity.FieldMilepostcode, milepostCode,
				MpmProjectTaskEntity.FieldTaskstate, MpmSystemConst.TaskStateCode.TaskNotStartCode, MpmSystemConst.TaskStateCode.TaskIncompleteCode);
		List<MpmProjectTaskEntity> mpmProjectTaskEntities = mpmProjectTaskService.queryByWhere(whereSql.toString());
		if (CollectionUtil.isEmpty(mpmProjectTaskEntities)) {
			whereSql.clear();
			whereSql.appendLineSingle(" {0} = {1} and enabled = 1 ", MpmProjectMilepostEntity.FieldOperatecode, milepostCode);
			MpmProjectMilepostEntity mpmProjectMilepostEntity = mpmProjectMilepostService.queryObjectByWhere(whereSql.toString());
			if (!ObjectUtil.isEmpty(mpmProjectMilepostEntity)) {
				mpmProjectMilepostEntity.setMilepoststate(1);
				mpmProjectMilepostEntity.setActualfinishtime(new Date());
				mpmProjectMilepostService.updateEntity(mpmProjectMilepostEntity);
				// 里程碑及任务置为已达成后需要执行实时更新项目状态方法（方法会判断是否需要更新）
				mpmProjectService.updateAbnormalState(projectCode);
			}
		}
		// 查询该任务对应的项目,判断是否需要更新项目状态至复盘阶段
		whereSql.clear();
		whereSql.appendSingle(" enabled = 1 and projectcode = '{0}' ", projectCode);
		MpmProjectEntity mpmProjectEntity = mpmProjectService.queryObjectByWhere(whereSql.toString());
		if(!ObjectUtil.isEmpty(mpmProjectEntity)) {
			mpmProjectTaskService.queryMilepostAllFinished(mpmProjectEntity);
		}
	}
}
